package de.uni_saarland.coli.doclists.index.docmentmanager;

import de.uni_saarland.coli.doclists.document.IndexDocument;
import de.uni_saarland.coli.doclists.document.iterator.IDocumentIterator;
import de.uni_saarland.coli.doclists.index.Index;
import de.uni_saarland.coli.doclists.index.filesystem.CachedObjectDiscList;
import de.uni_saarland.coli.doclists.texttools.ITextTokenizer;

public class DiscDocumentManager implements IDocumentManager {
	/**
	 * the documents
	 */
	CachedObjectDiscList<IndexDocument> docList;

	/**
	 * Constructor.
	 * 
	 * @param size
	 *            size of the cache
	 */
	public DiscDocumentManager(int size) {
		this.docList = new CachedObjectDiscList<IndexDocument>(size) {

			@Override
			public IndexDocument createObject(byte[] bytes) {
				return new IndexDocument(bytes);
			}
		};
	}

	@Override
	public void save(String path, String baseName) {
		this.docList.save(path, baseName);
	}

	@Override
	public void open(String path, String baseName) {
		this.docList.open(path, baseName);
	}

	@Override
	public void addDocuments(IDocumentIterator docIter,
			ITextTokenizer tokenizer, boolean addToTree) {
		while (docIter.hasNext()) {
			IndexDocument doc = new IndexDocument(docIter.next(), tokenizer);

			this.docList.addObject(doc);
			if (addToTree)
				Index.getTreeConstructor().addDocumentToSTree(doc);
		}
	}

	@Override
	public IndexDocument getDocument(int docID) {
		return this.docList.getObject(docID);
	}

	@Override
	public long numberOfDocuments() {
		return this.docList.indexSize();
	}

}
